package cn.edu.hpu.action;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.hpu.pojo.Contacts;
import cn.edu.hpu.pojo.Contactsnote;
import cn.edu.hpu.pojo.User;
import cn.edu.hpu.service.ContactsService;
import cn.edu.hpu.service.ContactsnoteService;
import cn.edu.hpu.service.UserService;
import cn.edu.hpu.util.TimeUtil;

import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ContactsnoteAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String id;
	private ContactsService contactsService;
	private Contacts contacts;
	private List<Contactsnote> cnlist;
	private ContactsnoteService contactsnoteService;
	private Contactsnote contactsnote;
	private String uid;
	private String cid;
	private UserService userService;
	private String cnid;
	private String content;
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	public String addcontactsnote(){
		try {
			/*Map<String, Object> map = ActionContext.getContext().getSession();
			Contacts contacts = (Contacts) map.get("contacts");*/
			cnlist = contactsnoteService.findAll(id);
			contacts = contactsService.findA(id);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	public String savecontactsnote(){
		try {
			contacts = contactsService.findA(cid);
			Map<String, Object> map = ActionContext.getContext().getSession();
			User u = (User) map.get("user");
			//User u = userService.findAUser(uid);
			Contactsnote cn = new  Contactsnote();
			cn.setCreatetime(TimeUtil.getTime());
			cn.setContent(content);
			cn.setUser(u);
			cn.setContacts(contacts);
			contactsnoteService.save(cn);
			
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("application/json;charset=UTF-8");
			Gson gson = new Gson();
			String[] array = {cn.getId(),cn.getContent(),cn.getCreatetime()};
			String json = gson.toJson(array);
			PrintWriter out = response.getWriter();
			out.print(json);
			out.flush();
			out.close();
	
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public String deletecontactsnote(){
		//Contactsnote cn = contactsnoteService
		contacts = contactsnoteService.findA(cnid).getContacts();
		cid = contactsnoteService.findA(cnid).getContacts().getId();
		System.out.println(cid);
		System.out.println(contacts);
		contactsnoteService.deletecontactsnote(cnid);
		return SUCCESS;
	}
	
	//setter,getter
	
	public String getId() {
		return id;
	}
	
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getCnid() {
		return cnid;
	}
	public void setCnid(String cnid) {
		this.cnid = cnid;
	}
	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public String getCid() {
		return cid;
	}
	public void setCid(String cid) {
		this.cid = cid;
	}
	public String getUid() {
		return uid;
	}
	public void setUid(String uid) {
		this.uid = uid;
	}
	public Contactsnote getContactsnote() {
		return contactsnote;
	}
	public void setContactsnote(Contactsnote contactsnote) {
		this.contactsnote = contactsnote;
	}
	public List<Contactsnote> getCnlist() {
		return cnlist;
	}
	public void setCnlist(List<Contactsnote> cnlist) {
		this.cnlist = cnlist;
	}
	@Autowired
	public void setContactsnoteService(ContactsnoteService contactsnoteService) {
		this.contactsnoteService = contactsnoteService;
	}
	public void setId(String id) {
		this.id = id;
	}
	
	public Contacts getContacts() {
		return contacts;
	}
	public void setContacts(Contacts contacts) {
		this.contacts = contacts;
	}
	@Autowired
	public void setContactsService(ContactsService contactsService) {
		this.contactsService = contactsService;
	}
	
	
}
